package com.internetCafes.spms.web.customer.service.impl.role;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.internetCafes.spms.web.customer.entity.role.RoleDeptDataAsso;
import com.internetCafes.spms.web.customer.mapper.role.RoleDeptDataAssoMapper;
import com.internetCafes.spms.web.customer.model.role.RoleDeptDataDo;
import com.internetCafes.spms.web.customer.model.role.RoleDeptDataPo;
import com.internetCafes.spms.web.customer.service.role.IRoleDeptDataAssoService;
import com.internetCafes.spms.web.sys.model.UserInfo;
import com.internetCafes.spms.web.tenant.model.podo.TenantDeptInfoPlusDo;
import com.internetCafes.spms.web.tenant.model.podo.TenantDeptInfoPlusPo;
import com.internetCafes.spms.web.tenant.service.ITenantDeptInfoPlusService;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色数据权限表 服务实现类
 * </p>
 *
 * @author Zj
 * @since 2021-10-13
 */
@Service
public class RoleDeptDataAssoServiceImpl extends ServiceImpl<RoleDeptDataAssoMapper, RoleDeptDataAsso> implements IRoleDeptDataAssoService {

    @Autowired
    private ITenantDeptInfoPlusService tenantDeptInfoPlusService;


    @Override
    public List<Long> listDeptIdAuthorAndUserDeptUnder(UserInfo userEntity) {
        List<Long> deptIdList = new ArrayList<>();
        List<Long> deptPoIdList
                = tenantDeptInfoPlusService.deptList(new TenantDeptInfoPlusDo().setAncestorId(userEntity.getTenantDeptId()))
                .stream().map(TenantDeptInfoPlusPo::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(deptPoIdList)) {
            deptIdList.addAll(deptPoIdList);
        }
        List<Long> assoDeptIdList = listPo(new RoleDeptDataDo().setUserId(userEntity.getId()))
                .stream().map(RoleDeptDataAsso::getDeptId)
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(assoDeptIdList)) {
            deptIdList.addAll(assoDeptIdList);
        }
        return deptIdList;
    }

    @Override
    public List<RoleDeptDataPo> listPo(RoleDeptDataDo roleDeptDataDo) {
        return baseMapper.listPo(roleDeptDataDo);
    }

    @Override
    public boolean removeByRoleId(Long roleId) {
        return remove(new LambdaQueryWrapper<RoleDeptDataAsso>().eq(RoleDeptDataAsso::getRoleId, roleId));
    }

    @Override
    public boolean updateRoleDeptList(Long roleId, List<Long> deptIdList) {
        // 移除原有部门关联
        removeByRoleId(roleId);

        // 重新绑定部门
        List<RoleDeptDataAsso> roleDeptAssoDoList = deptIdList.stream()
                .map(deptId -> new RoleDeptDataAsso().setDeptId(deptId).setRoleId(roleId))
                .collect(Collectors.toList());
        return saveBatch(roleDeptAssoDoList);
    }
}
